<template>
    <div>
        <div>
            <el-input placeholder="请输入角色英文名" v-model="role.name" prefix-icon="el-icon-plus" size="small" style="width: 300px">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input v-model="role.nameZh" @keydown.enter.native="doAddRole"  placeholder="请输入角色中文名称" prefix-icon="el-icon-plus" style="width: 300px;margin-left: 10px;margin-right: 10px" size="small"></el-input>
            <el-button type="primary" size="small" @click="doAddRole">添加角色</el-button>
        </div>
        <div style="margin-top: 10px;width: 700px">
            <el-collapse v-model="activeName" accordion @change="change">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete" @click="deleteRole(r)"></el-button>
                        </div>
                        <div>
                            <el-tree show-checkbox
                                     :data="allMenus"
                                     :props="defaultProps"
                                     ref="tree"
                                     :key="index"
                                     :default-checked-keys="selectedMenus"
                                     node-key="id">
                            </el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button size="mini"> 取消修改</el-button>
                                <el-button size="mini" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>

            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data(){
            return{
                role:{
                    name:'',
                    nameZh:''
                },

                roles:[],
                allMenus:[],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                selectedMenus:[],
                activeName:-1
            }
        },
        mounted(){
            this.initRoles();
        },
        methods:{
            deleteRole(role){
                this.$confirm('是否永久删除['+role.nameZh+']角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/permission/role/'+role.id ).then(resp=>{
                        if(resp){
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消删除'
                    });
                });
            },
            doAddRole(){
                if (this.role.name&&this.role.nameZh){
                    this.postRequest('/system/basic/permission/role',this.role).then(resp=>{
                        if(resp){
                            this.role.name='';
                            this.role.nameZh='';
                            this.initRoles();
                        }
                    })
                }else {
                    this.$message.error('所有字段不能为空不能为空');
                }
            },
            change(rid){
                //rid展示的是:name="r.id"
                if(rid){
                   this.initAllMenus();
                   this.initSelectMenus(rid);
                }
            },

            initSelectMenus(rid){
                this.getRequest('/system/basic/permission/mid/'+rid).then(resp=>{
                    if(resp){
                        this.selectedMenus = resp;
                    }
                })
            },
            initAllMenus(){
                this.getRequest('/system/basic/permission/menus').then(resp=>{
                    if(resp){
                        this.allMenus = resp;
                    }
                })
            },
            initRoles(){
                this.getRequest('/system/basic/permission/').then(resp=>{
                    if(resp){
                        this.roles = resp;
                    }
                })
            },
            doUpdate(rid,index){

              let tree = this.$refs.tree[index];
              let selectedKeys = tree.getCheckedKeys(true);
              let url = '/system/basic/permission/?rid=' + rid;
              selectedKeys.forEach(key=>{
                  url+='&mids='+key;
              });
              this.putRequest(url).then(resp=>{
                  if(resp){
                      this.activeName=-1;
                  }
              })
            }
        }
    }
</script>

<style scoped>

</style>